<!-- 角色与权限 -->
<template>
  <div>
    <pageHeader :title="title" :breadCrumb="breadCrumb" :show-back="true">
    </pageHeader>
    <div class="role-permission">
      <a-space class="search-box">
        <div>
          <a-space size="large">
            <div>
              <a-space>
                <div style="font-weight: bold">角色名称:</div>
                <a-input-search
                  search-button
                  :style="{ width: '220px' }"
                  placeholder="角色名称"
                />
              </a-space>
            </div>
            <!-- <div>
              <a-space>
                <div style="font-weight: bold">状态</div>
                <a-select :style="{ width: '120px' }">
                  <a-option :value="1">启用</a-option>
                  <a-option :value="2">停用</a-option>
                </a-select>
              </a-space>
            </div> -->
            <a-button type="outline">
              <template #icon> <icon-refresh /> </template>重置</a-button
            >
          </a-space>
        </div>
        <div style="padding-left: 15px">
          <a-space>
            <a-button @click="addRoleInfo" type="primary">
              <template #icon>
                <icon-plus />
              </template>
              <template #default>新增</template>
            </a-button>
            <a-button status="success">
              <template #icon>
                <icon-download />
              </template>
              <template #default>导入</template>
            </a-button>
            <a-button status="warning">
              <template #icon>
                <icon-upload />
              </template>
              <template #default>导出</template>
            </a-button>
          </a-space>
        </div>
      </a-space>
    </div>

    <div>
      <tableTurn :tableData="GetRoleData" :args="args" ref="tableTurnRef">
        <a-table-column :width="80" align="center">
          <template #title> 序号 </template>
          <template #cell="{ rowIndex }">
            {{ rowIndex + 1 }}
          </template>
        </a-table-column>
        <a-table-column
          v-for="item in columns"
          :key="item.code"
          :title="item.title"
          :data-index="item.code"
          align="center"
        >
          <template v-if="item.code === 'bu'" #cell="{ record }">
            {{ record.department.name }}
          </template>
          <!-- 状态item -->
          <template v-else-if="item.code === 'status'" #cell="{ record }">
            <div v-if="record.status === 1" class="y_statusBox">
              <span>启用</span>
            </div>
            <div v-else class="n_statusBox">
              <span>停用</span>
            </div>
          </template>
          <template v-else-if="item.code === 'createdAt'" #cell="{ record }">
            {{ dayjs(record.createdAt).format('YYYY-MM-DD') }}
          </template>
        </a-table-column>
        <a-table-column :width="300" align="center">
          <template #title> 操作 </template>
          <template #cell="{ record }"
            ><div
              ><a-button @click="putState(record.id)" type="text">{{
                record.status === 2 ? '启用' : '停用'
              }}</a-button>
              <a-button @click="getGainsingleRole(record.id)" type="text"
                >编辑</a-button
              >
              <a-popconfirm
                @ok="deletePosition(record.id)"
                content="确认删除?"
                type="error"
              >
                <a-button type="text">删除</a-button>
              </a-popconfirm></div
            ></template
          >
        </a-table-column>
      </tableTurn>
    </div>
    <addRole
      v-model:visible="modelVisible"
      :title="addRoleTitle"
      :formList="formList"
      @addRoleEmit="addRoleEmit"
    ></addRole>
    <!-- <div class="role-permission">
      <roleList class="role-list"></roleList>
      <roleDetail class="role-detail"></roleDetail>
    </div> -->
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import {
    gainRole,
    AddRole,
    delRole,
    putRoleState,
    gainsingleRole,
    medicalRole,
  } from '@/api/home/System settings/roles-permissions/index';
  import dayjs from 'dayjs';
  import addRole from './add-role/index.vue';

  const title: string = '角色与权限';
  const breadCrumb: string[] = ['首页', '系统设置', '角色与权限'];
  const tableTurnRef = ref();
  let formList = ref({
    menuIds: [],
    roleSort: 1,
    roleName: '',
    status: 1,
    remarks: '',
  });
  const addRoleTitle = ref('新建角色');
  const modelVisible = ref(false);
  const GetRoleData = async (pageData: any) => {
    try {
      const res = await gainRole({ ...pageData });
      return res;
    } catch (err: unknown) {
      console.log(err);
    }
  };

  const columns = [
    {
      title: '角色名称',
      code: 'roleName',
    },
    // {
    //   title: '权限字符',
    //   code: 'roleKey',
    // },
    {
      title: '状态',
      code: 'status',
    },
    {
      title: '创建时间',
      code: 'createdAt',
    },
  ];

  const deletePosition = async (id: number) => {
    await delRole({ id });
    tableTurnRef.value.loadData();
  };

  const addRoleInfo = () => {
    formList.value = {
      menuIds: [],
      roleSort: 1,
      roleName: '',
      status: 1,
      remarks: '',
    };
    modelVisible.value = true;
  };

  const putState = async (id: number) => {
    await putRoleState({ id });
    tableTurnRef.value.loadData();
  };

  const getGainsingleRole = async (id: number) => {
    const res = await gainsingleRole({ id });
    formList.value = res;
    modelVisible.value = true;
    addRoleTitle.value = '编辑角色';
    // const getData = toTree(res.roleMenu);
  };

  const addRoleEmit = async (formData: any, key: string) => {
    if (key === 'add') {
      await AddRole({ ...formData });
      tableTurnRef.value.loadData();
    } else {
      await medicalRole({ ...formData });
      tableTurnRef.value.loadData();
    }
  };
</script>

<style scoped lang="less">
  .role-permission {
    padding: 10px;
    // display: flex;
    .search-box {
      margin-bottom: 16px;
      display: flex;
      justify-content: space-between;
    }
  }
</style>
